var count = 0;
var clip = null;

function initZero() {
    clip = new ZeroClipboard.Client();
    clip.setHandCursor(true);
    clip.addEventListener('complete',
    function(client, text) {
        $('#copied').fadeIn('slow').delay(2000).fadeOut('fast');
    });
    clip.addEventListener('mouseOver',
    function(client) { // update the text on mouse over
        var newText = $('#linksDisp').html();
        newText = newText.replace(/\n/g, "\r\n");
        clip.setText(newText);
    });
    clip.glue('copyLinks');
}

function showMessage(succeeded, msg, url) {++count;
    var html;
    if (!$('.closeAllLink').is(":visible")) {
        $('.viewAllLink').fadeIn('slow');
        $('.closeAllLink').fadeIn('slow');
    }
    if (succeeded) html = '<div class="ok"><span class="counter"></span><span class="succeeded"></span><a href="#" class="closelink">close</a><a href="#" class="imagelink" target="_blank" >image link</a></div>';
    else html = '<div class="error"><span class="counter"></span></span><span class="failed"></span></div>';
    var newOne = $(html).appendTo('#result');
    newOne.find(succeeded ? '.succeeded': '.failed').text(msg);
    if (succeeded) newOne.find('.imagelink').attr("href", url);
    var theCounter = newOne.find('.counter');
    theCounter.append('#' + count);
    var newLink = newOne.find('.closelink');
    newLink.click(function() {
        $(this).parent().hide('slow',
        function() {
            $(this).remove();
        });
        return false;
    });
    return newOne;
}    $(document).ready(function() {
    $('.closeAllLink').click(function() {
        $('.ok').hide('slow',
        function() {
            $(this).remove();
        });
        $('.error').hide('slow',
        function() {
            $(this).remove();
        });
        $(this).fadeOut('slow');
        $('.viewAllLink').fadeOut('slow');
        return false;
    });
    $('.viewAllLink').click(function() {
        $('#allLinks').hide();
        $('#linksDisp').text('');
        $('.imagelink').each(function(index, element) {
            $('#linksDisp').append($(this).attr("href") + "\n");
        });
        $('#allLinks').fadeTo(0, 0);
        $('#allLinks').show('fast');
        $("html,body").animate({
            scrollTop: $("#allLinks").offset().top
        },
        'slow',
        function() {
            $('#allLinks').fadeTo('show', 1);
        });
        initZero();
        return false;
    });
    $('#uploader').append('<input type="file" name="file" id="file_upload" />');    $('#file_upload').uploadify({
        'fileTypeDesc': 'Image Files',
        'removeTimeout': 0,
        'fileTypeExts': '*.gif; *.jpg; *.png; *.jpeg',
        'fileSizeLimit': '<?php echo MAXImgSize; ?>B',
        'multi': true,
        'buttonText': 'Select File',
        'fileObjName': 'file',
        'swf'      : 'static/uploadify.swf',
        'uploader': 'upload.php',
        'onUploadError': function(file, errorCode, errorMsg, errorString) {
            alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
        },
                    'onUploadSuccess': function(file, data, response) {
            var jsonData = eval('(' + data + ')');
            var newOne = showMessage(jsonData.succeeded, jsonData.msg, jsonData.url);
            newOne.fadeIn('slow');
        }     
    });    $('#file_upload').ready(function(e) {
        $('#loading').fadeOut('fast',
        function() {
            $('#uploader').fadeIn('slow');
            $(this).remove();
        });
    });
    $('#allLinkClose').click(function() {
        $("#allLinks").hide('slow');
        $('#copied').hide();
		return false;
    });
});